from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 导入分页
from SaaSDevelTool.data_operation import paging_tool_join

"""功能包展示"""


class SaaSDevelModelPackAgeQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取页码
        page_number = request.GET.get("page_number")
        # 获取每页条数
        page_size = request.GET.get("page_size")
        # 获取功能包大模块
        model_id = request.GET.get("model_id")
        # 获取功能包名称
        package_name = request.GET.get("package_name")
        # 判断必传参数
        if not all([page_size, page_number]):
            return create_response(code=400, msg="页码于每页条数数据格式未传递或者传递错误。")

        # 定义初始查询语句
        where_package_sql = ""

        # 判断对应的大模块编号
        if model_id:
            where_package_sql = where_package_sql + """ AND p.model_id = {} """.format(model_id)
        # 判断功能包名称
        if package_name:
            where_package_sql = where_package_sql + """ AND p.package_name LIKE '%{}%' """.format(package_name)

        # 拼接sql语句进行查询
        query_sum_package_table_sql = """
                SELECT 
                   p.package_id,
                   p.package_name,
                   p.package_remark,
                   smt.model_name,
                   p.package_images
                FROM 
                    db_saas_center.sum_package_table p
                LEFT JOIN  
                    db_saas_center.sum_model_table smt 
                ON 
                    p.model_id = smt.model_id 
            WHERE 1=1 {}
            ;""".format(where_package_sql)
        # 调用方法查询语句
        query_sum_package_table_data = paging_tool_join(query_sum_package_table_sql, int(page_number), int(page_size))
        # 返回响应
        return create_response(code=200, msg="", data=query_sum_package_table_data)
